var _regions = new Array();
_regions[0] = "ef_region_inqu";
_regions[1] = "ef_region_result";
_regions[2] = "ef_region_solveadd";
_regions[3] = "ef_region_feedbackreport";
efform_onload = function() {

	InitRegionShow();
};

function InitRegionShow() {
	hideAllRegions(_regions);
	$('#ef_region_inqu').show();
	$('#ef_region_result').show();
};


/* 根据主表查询明细 */
efgrid_onRowClicked = function(grid_id, row_index) {
	if (grid_id == "ef_grid_result") {
		var grid = efgrid.getGridObject(grid_id);
		var selectedRow = grid.getRowData(row_index);
		var fdTccrId = grid.getCellValueByColumnName(row_index, "fdKeyId");
		var fdStatus = grid.getCellValueByColumnName(row_index, "fdStatus");
		// 隐藏/显示从表按钮
		if (fdStatus == '草拟')
			hideAndShowSubButtons(false);
		else
			hideAndShowSubButtons(true);

		var info = new EiInfo();
		info.set("fdTccrId", fdTccrId);
		$('#fdTccrId').val(fdTccrId);

		InitRegionShow();
		EiCommunicator.send("ZMCK01", "query", info, ajax_callback1);
	}
};
var ajax_callback1 = {
	onSuccess : function(eiInfo) {
		efwindow.hide();
		var grid = efgrid.getGridObject("ef_grid_feedbackreport");
		grid.refresh(eiInfo);
		$('#ef_region_feedbackreport').show();
	},
	onFail : function(eMsg) {
		EFAlert("明细记录查询失败，原因：" + eMsg, "警告");
	}
};
// 查询
button_query_onclick = function() {
	efgrid.submitInqu("ef_grid_result", "ZM", "ZMCK00", "query");
	clearRegion("ef_grid_feedbackreport");
	$("#ef_region_feedbackreport").hide();
};
// 新增
button_insert_onclick = function() {
	if (isSelected("ef_grid_result") == 0)
		return;
	efgrid.submitForm("ef_grid_result", "ZM", "ZMCK00", "insert", true);
};
//修改
button_update_onclick = function() {
	var grid = isSelected("ef_grid_result");
	if (grid == 0)
		return;

	var rows = grid.getCheckedRows();
	for (var i = 0; i < rows.length; i++) {
		if (grid.getCellValueByColumnName(rows[i], "fdStatus") != "草拟") {
			EFAlert("只能对草拟的记录进行修改！");
			return;
		}
	}
	efgrid.submitForm("ef_grid_result", "ZM", "ZMCK00", "update", true);
};
//删除
button_delete_onclick = function() {
	var grid = isSelected("ef_grid_result");
	if (grid == 0)
		return;

	var rows = grid.getCheckedRows();
	for (var i = 0; i < rows.length; i++) {
		if (grid.getCellValueByColumnName(rows[i], "fdStatus") != "草拟") {
			EFAlert("只能对草拟的记录进行删除！");
			return;
		}
	}

	EFConfirm('你确认要删除吗?', '确认框',
			function(r) {
				if (r)
					efgrid.submitForm("ef_grid_result", "ZM", "ZMCK00", "delete", true);
			});
};
//上报
button_report_onclick = function() {
	var grid = isSelected("ef_grid_result");
	if (grid == 0)
		return;

	var rows = grid.getCheckedRows();
	if (rows.length > 1) {
		EFAlert("一次只能上报一条记录！", "警告");
		return;
	}
	for (var i = 0; i < rows.length; i++) {
		if (grid.getCellValueByColumnName(rows[i], "fdStatus") != "草拟") {
			EFAlert("只能上报草拟状态的记录！", "警告");
			return;
		}
		if (grid.getCellValueByColumnName(rows[i], "fdKeyId") == "") {
			EFAlert("该记录不存在，请先新增后再上报！", "警告");
			return;
		}
	}

	EFConfirm('确认要上报吗?', '确认框',
			function(r) {
				if (r)
					efgrid.submitForm("ef_grid_result", "ZM", "ZMCK00",
							"report", true);
			});
};

//CLOSE 关闭 
button_close_onclick = function() {

//	var grid = isSelectedOne("ef_grid_result");
//	if (grid == 0)
//		return;
//
//	var rows = grid.getCheckedRows();
//	if (rows.length > 1) {
//		EFAlert("一次只能关闭一条记录！", "警告");
//		return;
//	}

	var grid=isSelectedOne("ef_grid_result");
	if (grid == 0)return; 
	var selectedRow=grid.getSelectRowsData()[0]; 

	for(var item in selectedRow){
		var colname=item;
		var colvalue=selectedRow[item];
		$('#solve_add-0-'+colname).val(colvalue); 
	} 
	
	var rows = grid.getCheckedRows();
	var status = grid.getCellValueByColumnName(rows[0], "fdStatus");
	if (status == "关闭") {
		EFAlert("该方案已关闭！", "警告");
		return;
	} else if (status != "草拟") {
		hideAllRegions(_regions);
		$('#ef_region_solveadd').show();
	} else {
		EFAlert("只能关闭已经上报的记录！", "警告");
	}
}; 

/**************************************************************************
 * ************************************************************************
 * 停产撤人方案总结 方法
 * ************************************************************************
 **************************************************************************/
button_solveconfirm_onclick = function()// CLOSE 主表 确定
{
	var isCheck = check();
  	if (isCheck == 0) {
        return;
    }
  	
	if (efvalidateDiv("ef_region_solveadd")) {
		var info = getDetail("ef_region_solveadd", "solve_add");
		if (null != info) {
			EiCommunicator.send("ZMCK00", "close", info, ajax_solveconfirm_callback);
			
		}
	}	
}; 
var ajax_solveconfirm_callback= {
	onSuccess: function(eiInfo) {
		var grid = efgrid.getGridObject("ef_grid_result");
		grid.refresh(eiInfo);//刷新主表grid
		InitRegionShow();
		$("#solve_add-0-fdZj").val("");
	},
	onFail:function(eMsg)
	{
		alert("确认失败，原因："+eMsg); 
	}
};

button_solvecancel_onclick = function ()//CLOSE 主表 返回
{
	InitRegionShow();
	$("#solve_add-0-fdZj").val("");
};


efgrid_afterAddNewRow = function(grid_id) {
	var grid = efgrid.getGridObject(grid_id);
	var rows = grid.getCheckedRows();
	if (grid_id == "ef_grid_result") {
		var fdMiningName = $('#fdMiningName').val();
		var fdCreator = $('#fdCreator').val();
		var day = getNowDate(); // 日
		for (var i = 0; i < rows.length; i++) {
			grid.getBlockData().setCell(rows[i], "fdKeyId", "");
			grid.getBlockData().setCell(rows[i], "fdStatus", "草拟"); // 状态
			grid.getBlockData().setCell(rows[i], "fdMiningName", fdMiningName);// 矿井名称
			grid.getBlockData().setCell(rows[i], "fdReportTime", day);// 汇报日期
			grid.getBlockData().setCell(rows[i], "fdReportPerson", fdCreator);// 汇报人
		}
	}

	if (grid_id == "ef_grid_feedbackreport") {
		var fdCreateDepartmentId = $('#fdCreateDepartmentId').val();
		var day = getNowDate(); // 日
		for (var i = 0; i < rows.length; i++) {
			grid.getBlockData().setCell(rows[i], "fdKeyId", "");
			grid.getBlockData().setCell(rows[i], "fdDepartmentId", fdCreateDepartmentId);// 责任部门
			grid.getBlockData().setCell(rows[i], "fdCurrentDate", day);// 当前日期
		}
	}
};

/**************************************************************************
 * ************************************************************************
 * 停产撤人方案反馈 方法
 * ************************************************************************
 **************************************************************************/
// 反馈
button_feedback_onclick = function() {
	if (isSelected("ef_grid_feedbackreport") == 0)
		return;
	efgrid.submitForm("ef_grid_feedbackreport", "ZM", "ZMCK01", "insert", true);
};

// 删除
button_delete1_onclick = function() {
	var grid = isSelected("ef_grid_feedbackreport");
	if (grid == 0)
		return;

	EFConfirm('你确认要删除吗?', '确认框', function(r) {
		if (r)
			efgrid.submitForm("ef_grid_feedbackreport", "ZM", "ZMCK01",
					"delete", true);
	});
};
// 根据主表选中记录的状态，确定从表的按钮是否显示
var hideAndShowSubButtons = function(status) {
	if (!status) {
		efbutton.setButtonStatus("feedback", false);
		efbutton.setButtonStatus("delete1", false);
	} else {
		efbutton.setButtonStatus("feedback", true);
		efbutton.setButtonStatus("delete1", true);
	}
};
//演习总结 输入项校验
function check() {
	var fdZj = $("#solve_add-0-fdZj").val().trim(); // 总结

	if (fdZj == "") {
		SCZXAlert("[停产撤人执行情况总结]不能为空！");
		return false;
	} else if (fdZj.getBytes() > 500 || fdZj.getBytes()< 1) {
		SCZXAlert("[停产撤人执行情况总结]请输入1~500个字符！");
		return false;
	}
	
	return true;
};
String.prototype.getBytes = function() {      
    var cArr = this.match(/[^\x00-\xff]/ig);      
    return this.length + (cArr == null ? 0 : cArr.length);   
      
};

String.prototype.trim=function(){
    return this.replace(/(^\s*)|(\s*$)/g, "");
 };
 efgrid_onAjaxSubmitSuccess = function(gridId, service_name, method_name, eiInfo) {
		if(gridId == "ef_grid_feedbackreport") {

			if (eiInfo.status != -1) {
				var grid = efgrid.getGridObject("ef_grid_feedbackreport");
				grid.refresh( eiInfo );
			}
			else
			{
				submitCallBackEiMsg = eiInfo.msg;
			}
		} else {
			var grid = efgrid.getGridObject(gridId);
			grid.refresh( eiInfo );
		}
	};
